/*
* Copyright© 2018 Shanxi Lu'an Taihang Laihui Net Tech co.,LTD.
*/
package com.laihuichuxing.netty.client;

import com.laihuichuxing.netty.common.core.model.Request;
import com.laihuichuxing.protobuf.module.DriverModule;
import com.laihuichuxing.protobuf.module.LbsModule;
import com.laihuichuxing.protobuf.module.UserModule;
import com.laihuichuxing.socket.LbsCmd;
import com.laihuichuxing.socket.LoginCmd;
import com.laihuichuxing.socket.ModuleId;
import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * Description:
 *
 * @author YangGuang
 * @since: 1.0
 * Create Date Time: 2018/10/9 11:19
 * Update Date Time:
 */
public class ClientMain {

    public static void main(String[] args) throws InterruptedException {
        Bootstrap bootstrap = new Bootstrap();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        ChannelFuture connect = bootstrap.group(workerGroup)
                .channel(NioSocketChannel.class)
                .handler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ch.pipeline().addLast(new RequestEncoder());
                        ch.pipeline().addLast(new ResponseDecoder());
                        ch.pipeline().addLast(new ClientHandler());
                    }
                }).connect(new InetSocketAddress("peer1", 10101));
        connect.sync();
        Request request = new Request();
        request.setModule(ModuleId.LOGIN);
        request.setCmd(LoginCmd.USER_LOGIN);
      //  UserModule.User user = UserModule.User.newBuilder().setType(1).setMobile("18837272773").setToken("15b734d1ec88b79fc13dba520b61601d").build();
        UserModule.User user = UserModule.User.newBuilder().setType(2).setMobile("13298172883").setToken("0cf6e1dd2841438aa1bc2c709624288d").build();
        request.setData(user.toByteArray());
        connect.channel().writeAndFlush(request);
        listenPosition(connect);
        //passengerOrder(connect);
        //driverArrival(connect);
        //passengerArrival(connect);
        //driverListen(connect);

        shutDown(workerGroup);
    }

    private static void shutDown(EventLoopGroup workerGroup) {
        workerGroup.shutdownGracefully();
    }

    private static void listenPosition(ChannelFuture connect) throws InterruptedException {
        Request request = new Request();
        Thread.sleep(2000);
        LbsModule.DriverPosition driverPosition = LbsModule.DriverPosition.newBuilder()
                .setCityCode("0351")
                .setLatitude("37794179")
                .setLongitude("112565108")
                .build();
        request.setModule(ModuleId.LBS);
        request.setCmd(LbsCmd.LISTEN_POSITION);
        request.setData(driverPosition.toByteArray());
        connect.channel().writeAndFlush(request);
    }

    private static void passengerOrder(ChannelFuture connect) throws InterruptedException {
        Request request = new Request();
        Thread.sleep(2000);
        LbsModule.PassengerPosition passengerOrder = LbsModule.PassengerPosition.newBuilder()
                .setCityCode("0351")
                .setLatitude("37794179")
                .setLongitude("112565108")
                .build();
        request.setModule(ModuleId.LBS);
        request.setCmd(LbsCmd.PASSENGER_ORDER);
        request.setData(passengerOrder.toByteArray());
        connect.channel().writeAndFlush(request);
    }

    private static void driverArrival(ChannelFuture connect) throws InterruptedException {
        Request request = new Request();
        Thread.sleep(2000);
        LbsModule.DriverPosition driverPosition = LbsModule.DriverPosition.newBuilder()
                .setCityCode("410100")
                .setLatitude("1111111")
                .setLongitude("3333333")
                .build();
        request.setModule(ModuleId.LBS);
        request.setCmd(LbsCmd.DRIVER_ARRIVAL);
        request.setData(driverPosition.toByteArray());
        connect.channel().writeAndFlush(request);
    }

    private static void passengerArrival(ChannelFuture connect) throws InterruptedException {
        Request request = new Request();
        Thread.sleep(2000);
        LbsModule.PassengerPosition passengerOrder = LbsModule.PassengerPosition.newBuilder()
                .setCityCode("410105")
                .setLatitude("37.777777")
                .setLongitude("117.777777")
                .build();
        request.setModule(ModuleId.LBS);
        request.setCmd(LbsCmd.PASSENGER_ARRIVAL);
        request.setData(passengerOrder.toByteArray());
        connect.channel().writeAndFlush(request);
    }

    private static void driverListen(ChannelFuture connect) throws InterruptedException {
        Thread.sleep(2000);
        DriverModule.DriverListen driverListen = DriverModule.DriverListen.newBuilder()
                .setToken("10ee272ee6d60bca83010a155d96c078")
                .setCityCode("0351").build();
        Request request = Request.valueOf(ModuleId.LBS,LbsCmd.DRIVER_LISTEN,driverListen.toByteArray());
        connect.channel().writeAndFlush(request);
    }
}
